﻿using Abp.AspNetCore.Mvc.Controllers;
using Batman.Core.Utils;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Text;

namespace Batman.Cms.Core.Controllers
{
    public class AdminController : AbpController
    {
        /// <summary>
        /// 菜单索引
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 数据列表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 表单页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit()
        {
            return View();
        }

        public IActionResult OALogin(string userName, string password)
        {
            if (Request.Method.ToUpper() == "POST")
            {
                var msg = string.Empty;
                var token = GetToken(userName, password, out var success);
                if (!success)
                {
                    return Json($"登陆失败:{token}");
                }

                var basName = GetTeam(userName, token, out success);
                if (!success)
                {
                    msg += $"获取部门失败，使用默认的厦门二组：{basName}";
                    basName = "厦门二组";
                }
                var res = CheckIn(userName, basName, token);
                msg += res;
                return Json(msg);
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// 登入注入
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private string GetToken(string userName, string password, out bool success)
        {
            var url = $"http://oa.kjlink.com/KjlinkPmsBackend/tbRsUser/login?userCode={userName}&password={password}";
            using (var client = new WebClient())
            {
                client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
                client.Headers.Add("DNT", "1");

                var res = client.DownloadString(url);
                var deRes = JsonUtil.Deserialize<dynamic>(res);
                if (deRes == null || deRes["token"] == null)
                {
                    success = false;
                    return res;
                }
                success = true;
                return deRes["token"];
            }
        }

        private string GetTeam(string userCode, string token, out bool success)
        {
            string url = $"http://oa.kjlink.com/KjlinkPmsBackend/tbGrPunchCard/selectBaseWorkTime?userCode={userCode}";
            using (var client = new WebClient())
            {
                client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
                client.Headers.Add("DNT", "1");
                client.Headers.Add("Token", token);

                var res = client.DownloadString(url);
                var deRes = JsonUtil.Deserialize<dynamic>(res);
                if (deRes == null || deRes["baseName"] == null)
                {
                    success = false;
                    return res;
                }
                success = true;
                return deRes["baseName"];
            }
        }


        /// <summary>
        /// 签入
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="placeName"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public string CheckIn(string userCode, string placeName, string token)
        {
            string url =
                $"http://oa.kjlink.com/KjlinkPmsBackend/tbGrPunchCard/checkIn?userCode={userCode}&placeName={placeName}";
            using (var client = new WebClient())
            {
                client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
                client.Headers.Add("DNT", "1");
                client.Headers.Add("Token", token);

                var str = JsonUtil.Serialize(new { });
                var bytes = Encoding.UTF8.GetBytes(str);
                var res = client.UploadData(url, "POST", bytes);
                return Encoding.UTF8.GetString(res);
            }
        }
    }
}
